package com.example.sixtrainingdemo.presenter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;

import androidx.annotation.NonNull;

import com.example.sixtrainingdemo.base.WeakRefHandler;
import com.example.sixtrainingdemo.contract.RoundContract;
import com.example.sixtrainingdemo.contract.TalkContract;
import com.example.sixtrainingdemo.entity.Round;
import com.example.sixtrainingdemo.entity.Talk;

import java.util.ArrayList;
import java.util.List;

public class TalkPresenter implements TalkContract.ITalkPresenter {

   private static final int WHAT_GET_Round_LIST = 0;
   private TalkContract.ITalkView mTalkView;
   private Context mContext;

   public TalkPresenter(TalkContract.ITalkView talkView, Context context) {
      mTalkView = talkView;
      mContext = context;
   }

   @Override
   public void getTalkList() {
      List<Talk> list = new ArrayList<>();
      new Thread(new Runnable() {
         @SuppressLint("Range")
         @Override
         public void run() {
            //此处异步进行数据查询
            //Uri uri = Uri.parse("content://com.example.sixtrainingdemo.provider/goods/1");
            Uri uri = Uri.parse("content://com.example.sixtrainingdemo.provider/talk");
            Cursor cursor = mContext.getContentResolver().query(
                    uri, null, null, null);
            try {
               if (cursor != null && cursor.moveToFirst()) {
                  do {
                     Talk talk = new Talk();
                     talk.setMessage(cursor.getString(cursor.getColumnIndex("talk_message")));
                     list.add(talk);
                  } while (cursor.moveToNext());
               }
            } finally {
               if (cursor != null) {
                  cursor.close();
               }
            }
            Uri photoUri = Uri.parse("content://com.example.sixtrainingdemo.provider/photo");
            Cursor photoCursor = mContext.getContentResolver().query(
                    photoUri, null, null, null);
            if (photoCursor != null && photoCursor.moveToFirst()) {
               do {
                  Log.d("", "photo_id = " + photoCursor.getInt(cursor.getColumnIndex("_id")));
                  Log.d("", "photo_miage = " + photoCursor.getBlob(2));
               } while (photoCursor.moveToNext());
            }
            //子线程通过Handler发送消息，完成线程之间的通信。
            Message message = Message.obtain();
            message.obj = list;
            message.what = WHAT_GET_Round_LIST;
            mHandler.sendMessage(message);
         }
      }).start();
   }

   Handler mHandler = new WeakRefHandler<>(Looper.getMainLooper(), this, new Handler.Callback() {
      @Override
      public boolean handleMessage(@NonNull Message msg) {
         int what = msg.what;
         switch (what) {
            case WHAT_GET_Round_LIST:
               mTalkView.showTalkList((List<Talk>) msg.obj);
               break;
         }
         return false;
      }
   });
}
